/*
 * @lc app=leetcode.cn id=3 lang=javascript
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
  const n = s.length;
  const chToIndexMap = new Map();

  let left = 0;
  let max = 0;
  for (let right = 0; right < n; right++) {
    const cur = s[right];
    if (chToIndexMap.has(cur)) {
      left = Math.max(left, chToIndexMap.get(cur) + 1);
    }
    chToIndexMap.set(cur, right);
    max = Math.max(max, right - left + 1);
  }
  return max;
};
// @lc code=end
